#!/usr/bin/python
# -*- coding: utf-8 -*-

#-------------------------------------------------
#Create At: 2008-6-8 下午11:33:47 
#Create By: Fla.sam
#
#DESC: Link bot database
#
#-------------------------------------------------

from sqlobject import *
import datetime
import types

class LinkItem( SQLObject ):
    
    itemName = StringCol( length=64, unique=True ) # name of link item
    itemUrlStr = StringCol( length=4096 ) # link item url
    itemInfoStr = StringCol( default='' ) # link item info or comment
    checkableBool = BoolCol( default=True ) #does this link can been checkable
    
    itemData = MultipleJoin( 'LinkItemData' )
    
    def _get_checkableBool( self ):
        if self._SO_get_checkableBool():
            return 'up'
        else:
            return 'down'    
        
    def _set_checkableBool( self, v ):
        if type( v ) is types.BooleanType:
            self._SO_set_checkableBool( v )
        else:
            if v.lower() == 'up':
                self._SO_set_checkableBool( True )
            elif v.lower() == 'down':
                self._SO_set_checkableBool( False )
            else:
                pass
    
class LinkItemData( SQLObject ):
    
    itemName = ForeignKey( 'LinkItem' )
    lastcheckDatetime = DateTimeCol(default=datetime.datetime.now) # link item last check datetime
    sizeLength = IntCol() # size of link item
    lastmodifieddatetimeStr = StringCol() # last modified datetime
    statsStr = StringCol( length=64) #link item status
    originalheaderStr = StringCol() # link item last original http header string
    itemhashStr = StringCol( length=256 )
        
    def _get_lastcheckDatetime(self):
        return self._SO_get_lastcheckDatetime().strftime('%y-%m-%d %H:%M:%S')

class LinkModified(SQLObject):
    
    itemName = ForeignKey('LinkItem')
    modifiedFrom = ForeignKey('LinkItemData')
    modifiedTo = ForeignKey('LinkItemData')
    
class LinkCmdHistory( SQLObject ):
    
    levelStr = StringCol( length=32 )
    cmdDatatime = DateTimeCol(default=datetime.datetime.now)
    cmdlineStr = StringCol()
    cmdresStatusStr = StringCol()
    
    def _get_lastcheckDatetime(self):
        return self._SO_get_lastcheckDatetime().strftime('%y-%m-%d %H:%M:%S')
